<?php

namespace JLPay\Services\Fund\Deferred;

use JLPay\Services\BaseService;
use JLPay\Models\Fund\Deferred\DeferredActivationQueryRequest;
use JLPay\Models\Fund\Deferred\DeferredActivationQueryResponse;
use JLPay\Models\Fund\Deferred\DeferredActivationOpenRequest;
use JLPay\Models\Fund\Deferred\DeferredActivationOpenResponse;
use JLPay\Models\Fund\Deferred\DeferredActivationCloseRequest;
use JLPay\Models\Fund\Deferred\DeferredActivationCloseResponse;
use JLPay\Models\Fund\Deferred\FundFlowQueryRequest;
use JLPay\Models\Fund\Deferred\FundFlowQueryResponse;
use JLPay\Models\Fund\Deferred\FundFreezeRequest;
use JLPay\Models\Fund\Deferred\FundFreezeResponse;
use JLPay\Models\Fund\Deferred\FundUnfreezeRequest;
use JLPay\Models\Fund\Deferred\FundUnfreezeResponse;
use JLPay\Models\Fund\Deferred\FundRefreezeRequest;
use JLPay\Models\Fund\Deferred\FundRefreezeResponse;

/**
 * 资金服务类
 */
class FundService extends BaseService
{
    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();

        if ($apiPath) {
            return $baseUrl . $apiPath;
        }

        // 使用请求类中定义的API_PREFIX和API_URI常量
        if (defined($requestClass . '::API_PREFIX') && defined($requestClass . '::API_URI')) {
            $apiPrefix = constant($requestClass . '::API_PREFIX');
            $apiUri = constant($requestClass . '::API_URI');
            
            // 组合API路径
            $path = ltrim($apiPrefix, '/') . '/' . ltrim($apiUri, '/');
            return $baseUrl . '/' . $path;
        }

        // 如果没有定义API_PREFIX和API_URI，使用默认路径
        return $baseUrl . '/open/fund/deferred/activation';
    }

    /**
     * 查询资金管控状态
     *
     * @param DeferredActivationQueryRequest $request 请求对象
     * @return DeferredActivationQueryResponse 响应对象
     */
    public function queryDeferredActivation(DeferredActivationQueryRequest $request): DeferredActivationQueryResponse
    {
        return $this->post($request, DeferredActivationQueryResponse::class);
    }

    /**
     * 开通资金管控
     *
     * @param DeferredActivationOpenRequest $request 请求对象
     * @return DeferredActivationOpenResponse 响应对象
     */
    public function openDeferredActivation(DeferredActivationOpenRequest $request): DeferredActivationOpenResponse
    {
        return $this->post($request, DeferredActivationOpenResponse::class);
    }

    /**
     * 关停资金管控
     *
     * @param DeferredActivationCloseRequest $request 请求对象
     * @return DeferredActivationCloseResponse 响应对象
     */
    public function closeDeferredActivation(DeferredActivationCloseRequest $request): DeferredActivationCloseResponse
    {
        return $this->post($request, DeferredActivationCloseResponse::class);
    }

    /**
     * 查询资金流水
     *
     * @param FundFlowQueryRequest $request 请求对象
     * @return FundFlowQueryResponse 响应对象
     */
    public function queryFundFlow(FundFlowQueryRequest $request): FundFlowQueryResponse
    {
        return $this->post($request, FundFlowQueryResponse::class);
    }

    /**
     * 冻结资金
     *
     * @param FundFreezeRequest $request 请求对象
     * @return FundFreezeResponse 响应对象
     */
    public function freezeFund(FundFreezeRequest $request): FundFreezeResponse
    {
        return $this->post($request, FundFreezeResponse::class);
    }

    /**
     * 解冻资金
     *
     * @param FundUnfreezeRequest $request 请求对象
     * @return FundUnfreezeResponse 响应对象
     */
    public function unfreezeFund(FundUnfreezeRequest $request): FundUnfreezeResponse
    {
        return $this->post($request, FundUnfreezeResponse::class);
    }

    /**
     * 续冻资金
     *
     * @param FundRefreezeRequest $request 请求对象
     * @return FundRefreezeResponse 响应对象
     */
    public function refreezeFund(FundRefreezeRequest $request): FundRefreezeResponse
    {
        return $this->post($request, FundRefreezeResponse::class);
    }
} 